package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.PayFlow;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.mapper.PayOrderMapper;
import cn.itsource.ymcc.service.IPayFlowService;
import cn.itsource.ymcc.service.IPayOrderService;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;
import sun.security.smartcardio.SunPCSC;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Eric Chen
 * @since 2022-07-23
 */
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {
    @Autowired
    private IPayOrderService payOrderService;
    @Autowired
    private IPayFlowService payFlowService;
    @Override
    public PayOrder selectByOrderNo(String orderNo) {
        EntityWrapper<PayOrder> wrapper = new EntityWrapper<>();
        wrapper.eq("order_no",orderNo);
        PayOrder payOrder = payOrderService.selectOne(wrapper);
        return payOrder;
    }

    @Override
    public void updateOrderAndFlow(PayFlow payFlow, PayOrder payOrder) {
        payOrderService.updateById(payOrder);
        payFlowService.insert(payFlow);
    }

    @Override
    public void cancel(String s) {
        PayOrder payOrder = selectByOrderNo(s);
        if (payOrder.getPayStatus() != 0)return;

        payOrder.setPayStatus(2);
        payOrder.setUpdateTime(new Date());

        //支付网关关闭
        try {
            AlipayTradeCloseResponse response = Factory.Payment.Common().close(s);
            System.out.println("超时关闭");
            //fail and again
        }catch (Exception e){
            e.printStackTrace();

        }


    }
}
